using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Banshee.Base.Winforms.Controls
{
    public partial class SearchEntry : UserControl
    {
        private ArrayList searchFields;
        public event EventHandler Changed;
        public event EventHandler EnterPress;
        public ArrayList SearchFields
        {
            get { return searchFields; }
            set
            {
                searchFields = value;
            }
        }
        public SearchEntry()
        {
            SetStyle(ControlStyles.OptimizedDoubleBuffer| ControlStyles.AllPaintingInWmPaint, true);

            InitializeComponent();
            this.Width = 200;
        }
        void BuildMenu()
        {
            foreach (string menuLabel in searchFields)
            {
                ToolStripItem item = null;
                if (menuLabel.Equals("-"))
                {
                    filter_menu.Items.Add(new ToolStripSeparator());
                }
                else
                {
                    item = new ToolStripMenuItem(menuLabel);
                    item.Click += new EventHandler(item_Click);
                    filter_menu.Items.Add(item);
                }
            }
        }
        ToolStripMenuItem activeItem;
        void item_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem item = sender as ToolStripMenuItem;
            if (activeItem == item)
            {
                item.Checked = true;
            }
            else
            {
                activeItem = item;
            }

            foreach (ToolStripItem iterItem in filter_menu.Items)
            {
                if (!(iterItem is ToolStripMenuItem))
                {
                    continue;
                }

                ToolStripMenuItem checkItem = iterItem as ToolStripMenuItem;

                if (checkItem != activeItem)
                {
                    checkItem.Checked = false;
                }
            }

            activeItem.Checked = true;

            search_text.SelectAll();

            OnChanged(this, new EventArgs());
        }

        protected virtual void OnChanged(object o, EventArgs args)
        {
            if (Ready)
            {
                EventHandler handler = Changed;
                if (handler != null)
                {
                    handler(o, args);
                }
            }
        }
        private void OnEntryActivated(object o, EventArgs args)
        {
            EventHandler handler = EnterPress;
            if (handler != null)
            {
                handler(this, new EventArgs());
            }
        }
        public string Query
        {
            get
            {
                return search_text.Text.Trim();
            }

            set
            {
                search_text.Text = value;
            }
        }

        public string Field
        {
            get
            {
                string field = string.Empty;
                for (int i = 0; i < filter_menu.Items.Count; i++)
                {
                    if (filter_menu.Items[i].GetType() != typeof(ToolStripSeparator))
                    {
                        ToolStripMenuItem item = (ToolStripMenuItem)filter_menu.Items[i];
                        if (item.Checked)
                            field = item.Text;
                    }

                }
                return field;
            }
            set
            {
                for (int i = 0; i < filter_menu.Items.Count; i++)
                {
                    if (filter_menu.Items[i].GetType() != typeof(ToolStripSeparator))
                    {
                        ToolStripMenuItem item = (ToolStripMenuItem)filter_menu.Items[i];
                        if (item.Text == value)
                            item.Checked = true;
                        else
                            item.Checked = false;
                    }
                }
            }
        }
        public void FocusSearch()
        {
            search_text.Focus();
        }

        public bool HasFocus
        {
            get
            {
                return search_text.Focused;
            }
        }

        public bool IsQueryAvailable
        {
            get
            {
                return Query != null && Query != String.Empty;
            }
        }
        private bool ready;
        public bool Ready
        {
            get { return ready; }
            set { ready = value; }
        }

        private void search_text_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.Handled = true;
                e.Handled = true;
                OnChanged(sender, null);
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            filter_menu.Show(search_text, new Point(0, search_text.Height));
        }
        public void CancelSearch()
        {
            ToolStripMenuItem first = filter_menu.Items[0] as ToolStripMenuItem;
            if (!first.Checked)
                first.Checked = true;
            search_text.Text = string.Empty;
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            CancelSearch();
        }

        private void SearchEntry_Load(object sender, EventArgs e)
        {
            ArrayList fields = new ArrayList();
            fields.Add("All");
            fields.Add("-");
            fields.Add("Song Name");
            fields.Add("Artist Name");
            fields.Add("Album Title");
            fields.Add("Genre");
            fields.Add("Year");
            searchFields = fields;
            BuildMenu();
            activeItem = filter_menu.Items[0] as ToolStripMenuItem;
        }

    }
}
